import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.UIManager;

import nauja.utils.jcalendar.JCalendar;
import nauja.utils.jcalendar.components.DefaultTitlePanel;
import nauja.utils.jcalendar.listeners.CalendarEvent;
import nauja.utils.jcalendar.listeners.DateChangeListener;
import nauja.utils.jcalendar.renderers.DefaultCalendarRenderer;
import nauja.utils.jcalendar.renderers.DefaultCalendarTitleRenderer;

/**
 * Example on how to use <code>DateChangeListener</code>.
 * @author Jeremy Morosi
 * @version 1.0
 */
@SuppressWarnings("serial")
public class ExampleDateChangeListener extends JPanel implements ActionListener, DateChangeListener {


	//***********************************************
	//              MAIN
	//***********************************************

	/**
	 * Main function.
	 * @param args arguments.
	 */
	public static void main(String[] args) {
		setLookAndFeel();
		JFrame frame = new JFrame();
		frame.setTitle("Current date");
		// Create the calendar.
		frame.setContentPane(new ExampleDateChangeListener());
		// End.
		frame.pack();
		frame.setMinimumSize(frame.getSize());
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}
	
	
	//***********************************************
	//              VARIABLES
	//***********************************************
	
	/**
	 * Calendar instance.
	 */
	private JCalendar calendar;
	/**
	 * Label for selected date.
	 */
	private JLabel labelDate;
	/**
	 * Button to add the listener.
	 */
	private JButton buttonAdd;
	/**
	 * Button to remove the listener.
	 */
	private JButton buttonRemove;
	

	//***********************************************
	//              CONSTRUCTORS
	//***********************************************
	
	/**
	 * Default constructor.
	 */
	public ExampleDateChangeListener() {
		super();
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		labelDate = new JLabel("Date selected: ");
		calendar = JCalendar.getInstance();
		calendar.addDateChangeListener(this);
		DefaultCalendarRenderer renderer = (DefaultCalendarRenderer) calendar.getCalendarRenderer();
		renderer.setTitleRenderer(new DefaultCalendarTitleRenderer(DefaultTitlePanel.MODIFY_MONTH | DefaultTitlePanel.EDITABLE));
		calendar.updateCalendar();
		buttonAdd = new JButton("Add listener");
		buttonAdd.setEnabled(false);
		buttonAdd.addActionListener(this);
		buttonRemove = new JButton("Remove listener");
		buttonRemove.setEnabled(true);
		buttonRemove.addActionListener(this);
		// Panel for label.
		JPanel panelLabel = new JPanel();
		panelLabel.setLayout(new BorderLayout());
		panelLabel.add(labelDate, BorderLayout.CENTER);
		// Panel for buttons.
		JPanel panelButtons = new JPanel();
		panelButtons.setLayout(new FlowLayout(FlowLayout.RIGHT));
		panelButtons.add(buttonAdd);
		panelButtons.add(buttonRemove);
		// Add everything.
		add(calendar);
		add(new JSeparator(JSeparator.HORIZONTAL));
		add(panelLabel);
		add(new JSeparator(JSeparator.HORIZONTAL));
		add(panelButtons);
		add(Box.createRigidArea(new Dimension(300, 0)));
	}
	
	
	//***********************************************
	//              GLOBAL FUNCTIONS
	//***********************************************

	/**
	 * Modify look and feel.
	 */
	public static void setLookAndFeel() {
		try {
			UIManager.setLookAndFeel(
				UIManager.getSystemLookAndFeelClassName());
		} catch(Exception e) {}
	}


	//***********************************************
	//              FUNCTIONS - EVENTS
	//***********************************************
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void dateChanged(CalendarEvent event) {
		if(event.isDateSelection()) {
			labelDate.setText("Date selected: " + calendar.format("dd MMMM yyyy (EEEE)"));
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void actionPerformed(ActionEvent event) {
		JButton source = (JButton) event.getSource();
		if(source == buttonRemove) {
			buttonRemove.setEnabled(false);
			calendar.removeDateChangeListener(this);
			buttonAdd.setEnabled(true);
		} else if(source == buttonAdd) {
			buttonAdd.setEnabled(false);
			calendar.addDateChangeListener(this);
			buttonRemove.setEnabled(true);
		}
	}
	
}